package com.wyh.service.serviceImpl.groupOne.customer;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.wyh.common.enums.ResultCodeEnum;
import com.wyh.common.exception.CustomException;
import com.wyh.domain.vo.groupOne.customer.MasterDataCustomerVO;
import com.wyh.domain.vo.groupOne.customer.InvoiceconfigurationVO;
import com.wyh.mapper.groupOne.customer.MasterDataCCustomerMapper;
import com.wyh.mapper.groupOne.customer.MasterDataInvoiceconfigurationMapper;
import com.wyh.service.groupOne.customer.MasterDataInvoiceconfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MasterDataMasterDataInvoiceconfigurationServiceImpl implements MasterDataInvoiceconfigurationService {

    @Autowired
    private MasterDataInvoiceconfigurationMapper masterDataInvoiceconfigurationMapper;

    @Autowired
    private MasterDataCCustomerMapper MasterDataCCustomerMapper;

    @Override
    public InvoiceconfigurationVO selectByCustomerId(Integer id) {
        if (id != null) {
            MasterDataCustomerVO dbcustomer = MasterDataCCustomerMapper.selectDetailBasic(id);
            if (!ObjectUtil.isEmpty(dbcustomer)) {
                return masterDataInvoiceconfigurationMapper.selectByCustomerId(id);
            }
        }
        throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
    }

    @Override
    public int insert(InvoiceconfigurationVO invoiceconfigurationVO) {
        MasterDataCustomerVO dbcustomer = MasterDataCCustomerMapper.selectDetailBasic(invoiceconfigurationVO.getCustomerid());
        InvoiceconfigurationVO dbinvoiceconfigurationVO = masterDataInvoiceconfigurationMapper.selectByCustomerId(invoiceconfigurationVO.getCustomerid());
        if (ObjectUtil.isEmpty(dbinvoiceconfigurationVO)) {
            if (!ObjectUtil.isEmpty(dbcustomer)) {
                DateTime date = DateUtil.date();
                dbcustomer.setLastupdatetime(date.toString());
                dbcustomer.setUserid(invoiceconfigurationVO.getUserid());
                MasterDataCCustomerMapper.update(dbcustomer);
                return masterDataInvoiceconfigurationMapper.insert(invoiceconfigurationVO);
            }
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        throw new CustomException(ResultCodeEnum.PROJECT_NAME_EXIST);

    }

    @Override
    public int update(InvoiceconfigurationVO invoiceconfigurationVO) {
        InvoiceconfigurationVO dbinvoiceconfigurationVO = masterDataInvoiceconfigurationMapper.selectById(invoiceconfigurationVO.getId());
        MasterDataCustomerVO dbcustomer = MasterDataCCustomerMapper.selectDetailBasic(dbinvoiceconfigurationVO.getCustomerid());
        if (!ObjectUtil.isEmpty(dbcustomer)) {
            DateTime date = DateUtil.date();
            dbcustomer.setLastupdatetime(date.toString());
            dbcustomer.setUserid(invoiceconfigurationVO.getUserid());
            MasterDataCCustomerMapper.update(dbcustomer);
            return masterDataInvoiceconfigurationMapper.update(invoiceconfigurationVO);
        }
        throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
    }


}
